package com.smcdd.cdb.dbo.cdb_dbo.ui.widget;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ActionItem;

import java.util.ArrayList;

/**
 * 作者：Administrator on 2017/7/13 14:14
 * 邮箱：zhanghuaiha@gmail.com
 *    门店popupWindow
 */

public class MenDianPopup extends PopupWindow {
    private Context mContext;

    // 列表弹窗的间隔
    protected final int LIST_PADDING = 10;

    // 实例化一个矩形
    private Rect mRect = new Rect();

    // 坐标的位置（x、y）
    private final int[] mLocation = new int[2];

    // 屏幕的宽度和高度
    private int mScreenWidth, mScreenHeight;

    // 判断是否需要添加或更新列表子类项
    private boolean mIsDirty ;

    // 位置不在中心
    private int popupGravity = Gravity.NO_GRAVITY;

    // 弹窗子类项选中时的监听
    private OnItemOnClickListener mItemOnClickListener;

    // 定义列表对象
    private TextView pop_page_title;
    private TextView pop_md_wifi,pop_md_mima;
    private TextView pop_page_cancel,pop_page_save;
    private String type0;

    // 定义弹窗子类项列表
    private ArrayList<ActionItem> mActionItems = new ArrayList<ActionItem>();

    public MenDianPopup(Context context) {
        // 设置布局的参数
        this(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public MenDianPopup(Context context, int width, int height) {
        this.mContext = context;

        // 设置可以获得焦点
        setFocusable(true);
        // 设置弹窗内可点击
        setTouchable(true);
        // 设置弹窗外可点击
        setOutsideTouchable(true);

        // 获得屏幕的宽度和高度
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        mScreenWidth = wm.getDefaultDisplay().getWidth();
        mScreenHeight = wm.getDefaultDisplay().getHeight();

        // 设置弹窗的宽度和高度
        setWidth(width);
        setHeight(height);

        setBackgroundDrawable(new BitmapDrawable());

        // 设置弹窗的布局界面
        setContentView(LayoutInflater.from(mContext).inflate(
                R.layout.popup_mendian_title,null));
        setAnimationStyle(R.style.AnimHead);
        initUI();
    }

    /**
     * 初始化弹窗列表
     */
    private void initUI() {
        pop_page_title = (TextView) getContentView().findViewById(R.id.pop_page_title);
        pop_page_cancel = (TextView) getContentView().findViewById(R.id.pop_page_cancel);
        pop_page_save = (TextView) getContentView().findViewById(R.id.pop_page_save);
        pop_md_wifi = (TextView) getContentView().findViewById(R.id.pop_md_wifi);
        pop_md_mima = (TextView) getContentView().findViewById(R.id.pop_md_mima);
        type0 = "-1";


//        if (mItemOnClickListener != null)
//            mItemOnClickListener.onItemClick(scan);
        pop_page_cancel.setOnClickListener(v -> dismiss());
        pop_page_save.setOnClickListener(v -> {

            if (mItemOnClickListener != null){
                mItemOnClickListener.onItemClick(type0);
            }
            dismiss();
        });
        //默认排序
        pop_page_title.setOnClickListener(v -> {
            type0 = "0";
            pop_page_title.setSelected(true);
            pop_md_wifi.setSelected(false);
            pop_md_mima.setSelected(false);
        });

        //WiFi店铺
        pop_md_wifi.setOnClickListener(v -> {
            type0 = "1";
            pop_md_wifi.setSelected(true);
            pop_md_mima.setSelected(false);
            pop_page_title.setSelected(false);
        });
        //密码店铺
        pop_md_mima.setOnClickListener(v -> {
            type0 = "4";
            pop_md_wifi.setSelected(false);
            pop_md_mima.setSelected(true);
            pop_page_title.setSelected(false);
        });
    }


    /**
     * 显示弹窗列表界面
     */
    public void show(View view) {
        // 获得点击屏幕的位置坐标
        view.getLocationOnScreen(mLocation);

        // 设置矩形的大小
        mRect.set(mLocation[0], mLocation[1], mLocation[0] + view.getWidth(),
                mLocation[1] + view.getHeight());

        // 判断是否需要添加或更新列表子类项
        populateActions();
        if (mIsDirty) {
        }

        // 显示弹窗的位置
        showAtLocation(view, popupGravity, mScreenWidth - LIST_PADDING
                - (getWidth() / 2), mRect.bottom);
    }

    /**
     * 设置弹窗列表子项
     */
    private void populateActions() {
        mIsDirty = false;

    }

    /**
     * 添加子类项
     */
    public void addAction(ActionItem action) {
        if (action != null) {
            mActionItems.add(action);
            mIsDirty = true;
        }
    }

    /**
     * 清除子类项
     */
    public void cleanAction() {
        if (mActionItems.isEmpty()) {
            mActionItems.clear();
            mIsDirty = true;
        }
    }

    /**
     * 根据位置得到子类项
     */
    public ActionItem getAction(int position) {
        if (position < 0 || position > mActionItems.size())
            return null;
        return mActionItems.get(position);
    }

    /**
     * 设置监听事件
     */
    public void setItemOnClickListener(
            OnItemOnClickListener onItemOnClickListener) {
        this.mItemOnClickListener = onItemOnClickListener;
    }

    /**
     * @author yangyu 功能描述：弹窗子类项按钮监听事件
     */
    public static interface OnItemOnClickListener {
        public void onItemClick(String type0);
    }
}
